local Obj = require("core.Obj")
local ObjHuman = require("core.ObjHuman")
local Skill = require("skill.Skill")
local SkillExcel = require("excel.Skill").Skill
local Cmds  = require("buffer.Cmds")
local Buffer = require("buffer.Buffer")
local BagLogic = require("bag.BagLogic")
local BaowuLogic = require("baowu.BaowuLogic")


Conditions = {}
Tips = {}
local skillID
function handler(obj,list,skillid)
	skillID = skillid
	-- 条件判断
	for _,loss in ipairs(list) do
		local callback = Conditions[loss[1]]
		
		if obj.obj_type~=Obj.TYPE_HUMAN then					--非人(怪物等)
			if callback==Conditions.hp  and not callback(obj,loss[2],loss[3],loss[4], skillid) then
				return
			end
		else
			if not callback(obj,loss[2],loss[3],loss[4], skillid) then	--人物
				return false,Tips[loss[1]](obj,loss[2],loss[3],loss[4])
			end
		end
	end
	
	-- 消耗
	for _,loss in ipairs(list) do
		local callback = _ENV[loss[1]]
		
		if obj.obj_type~=Obj.TYPE_HUMAN then					--非人(怪物等)
			if callback==hp then
				callback(obj,loss[2],loss[3],loss[4])
			end
		else
			callback(obj,loss[2],loss[3],loss[4])
		end
	end
	
	return true
end

function Conditions.mp(obj,value,bufID)
	value = Cmds.passive.skillMP(obj,value)
	value = Cmds.passive.skillNoMP(obj,value,skillID)
	local flag = obj.db.mp>=value
	if not flag then
		if bufID then
			Buffer.removeID(bufID,obj)		
		end
	end
	return flag
end

function mp(obj,value)
	value = Cmds.passive.skillMP(obj,value)
	value = Cmds.passive.skillNoMP(obj,value,skillID)
	local currentValue = obj.db.mp
	ObjHuman.setMp(obj,currentValue-value)
	
	ObjHuman.sendAreaHpMp(obj)
end

function Tips.mp(obj,value)
	return "气力值不足"..value
end

function Conditions.hp(obj,value,bufID)
	local flag = Obj.getHp(obj)>value
	if not flag then
		if bufID then
			Buffer.removeID(bufID,obj)		
		end
	end
	return flag
end

function hp(obj,value)
	Skill.addHp(obj, -value,obj.id)
end

function Tips.mp(obj,value)
	return "法力值不足"..value
end

function Conditions.money(obj,value)
	local nowYinliang = ObjHuman.getYinliang(obj)
	return nowYinliang>=value
end

function money(obj,value)
	ObjHuman.decYinliang(obj,value,"skill_upgrade")
end

function Tips.money()--obj,value)
--	return "银两不足"..value
    return "所需银两或道具不足！"
end

function Conditions.sblevel(obj,value)
    local skill = SkillExcel[skillID]
    if not skill.sb then
        return 
    end
    local sbData = ObjHuman.getShenbing(obj, skill.sb)
    if not sbData then
        return
    end
    return sbData.lv >= value 
end

function sblevel()--obj, value)
    
end
function Tips.sblevel(obj,value)
    local skill = SkillExcel[skillID]
    if not skill.sb then
        return "error1"
    end
    local sbData = ObjHuman.getShenbing(obj, skill.sb)
    if not sbData then
        return "神兵未激活："..skill.sb
    end
    return "神兵强化等级不够"..value
end



function Conditions.mpRate(obj,value)
	return obj.db.mp/ObjHuman.getMpMax(obj)>=value
end

function mpRate(obj,value)
	ObjHuman.setMp(obj,Obj.obj.db.mp-ObjHuman.getMpMax(obj)*value)
end

function Tips.mpRate(obj,value)
	return "当前气力值百分比不足"..value*100 .."%"
end

function Conditions.hpRate(obj,value)
	return Obj.getHp(obj)/Obj.getHpMax(obj)>value
end

function hpRate(obj,value)
	Skill.addHp(obj, -Obj.getHpMax(obj)*value, obj.id)
end

function Tips.mpRate(obj,value)
	return "当前气力值百分比不足"..value*100 .."%"
end

function Conditions.level(obj,value)
	return Obj.getLv(obj)>=value
end

function level()--obj,value)	
end

function Tips.level(obj,value)
	return "等级不足"..value .."级"
end

function Conditions.item(obj,itemID,count)	
	return BagLogic.getItemCount(obj,itemID)>=count
end

function item(obj,itemID,count)
	BagLogic.delItemsById(obj,itemID,count,"skill_upgrade")
end

function Tips.item()--obj,itemID,count)
    return "所需银两或道具不足！"
end


function Conditions.zhangong(obj, zhangong)
  return obj.db.zhangong >= zhangong
end
function zhangong(obj, zhangong)
  ObjHuman.decZhangong(obj, zhangong, "skill_upgrade")
end
function Tips.zhangong()--obj, zhangong)
  return "所需战功不足！"
end

function Conditions.petlevel(obj,value,petid)
    local data = ObjHuman.getPet(obj, petid)
    return data.lv >= value
end
function petlevel(obj, value, petid)
  return obj, value, petid
end
function Tips.petlevel(obj, value)
  return "神将等级未达到"..value.."级"
end
function Conditions.horseLv(obj, value)
  local starLv = obj.db.horseStarLv
  local lv = math.floor(starLv / 10) + 1
  local stage = obj.db.horseStageLv
  if math.floor(starLv/100) == stage then
    lv = lv - 1
  end
  return lv >= value
end
function horseLv(obj)
  return obj
end
function Tips.horseLv(obj, value)
  return "坐骑等级未达到"..value.."级"
end

function Conditions.baowuLv(obj, value, a, b, skillId)
  return BaowuLogic.getBaowuLv(obj, skillId) >= value
end

function Tips.baowuLv(obj, value)
  return "宝物等级未达到"..value.."级"
end

function baowuLv(obj, value)
  return obj
end